/*
 * @Author: 肖芬芳 1911683843@qq.com
 * @Date: 2022-09-23 16:18:32
 * @LastEditors: 肖芬芳 1911683843@qq.com
 * @LastEditTime: 2022-12-13 09:51:02
 * @FilePath: \node_study\routes\admin\system\role\list.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const {
    SecondClass
} = require('../../../../model/goodSecondClass');

module.exports = async (req, res) => {
    // 获取post参数
    let {
        pageSize,
        currentPage,
        firstClassId,
        ...secondClassSearch
    } = req.fields
    // console.log(secondClassSearch.startTime)
    let secondClass = []
    if (JSON.stringify(secondClassSearch) == '{}') {
        // 查询所有一级分类，逆序展示
        secondClass = await SecondClass.find({
            'firstClassId': firstClassId
        }).sort('-createTime').skip((currentPage - 1) * pageSize).limit(pageSize);
    } else {
        //模糊查询，生成正则表达式new RegExp(req.fields.role)
        secondClass = await SecondClass.find({
            $and: [{
                    'firstClassId': firstClassId
                },
                {
                    'secondClassName': new RegExp(secondClassSearch.secondClassName)
                },
                {
                    'createTime': {
                        //数字最大值：Number.MAX_VALUE
                        $gt: secondClassSearch.startTime == undefined || null ? 0 : secondClassSearch.startTime,
                        $lt: secondClassSearch.endTime == undefined || null ? Number.MAX_VALUE : secondClassSearch.endTime
                    }
                }
            ]
        }).sort('-createTime').skip((currentPage - 1) * pageSize).limit(pageSize);
    }

    let totalCount = secondClass.length;
    res.send({
        'code': 0,
        "totalCount": totalCount,
        "data": secondClass
    });
}